package cn.xz.mxxz.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.xz.mxxz.R;
import cn.xz.mxxz.bean.Hyc;
import cn.xz.mxxz.util.AppHelper;
import cn.xz.mxxz.widgets.SlideBtnItemView;

/**
 * Created by yangkun on 2019/1/7.
 */

public class HycZhAdapter extends RecyclerView.Adapter<HycZhAdapter.zuohuaViewHolder> implements SlideBtnItemView.IonSlidingButtonListener {
    private Context mContext;
    private SlideBtnItemView mMenu = null;
    private ZuohuaSlidingViewClickListener mZuohuaSlidingViewClickListener;
    private List<Hyc> bfList;
    private int selectPosition = -1;

    public HycZhAdapter(Context context, ZuohuaSlidingViewClickListener mZuohuaSlidingViewClickListener, List<Hyc> str) {
        mContext = context;
        this.mZuohuaSlidingViewClickListener = mZuohuaSlidingViewClickListener;
        this.bfList = str;
    }

    public void upData(List<Hyc> str){
        this.bfList = str;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public zuohuaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_wel_list_layout, parent, false);
        zuohuaViewHolder mZuohuaViewHolder = new zuohuaViewHolder(view);
        return mZuohuaViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final zuohuaViewHolder holder, int position) {
        holder.zuohuaTv.setText(bfList.get(position).getTitle());
        holder.zuohuaRel.getLayoutParams().width = AppHelper.dp2px(800f);//1143
        holder.zuohuaTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int n = holder.getLayoutPosition();
                mZuohuaSlidingViewClickListener.onItemClick(view, n);
                selectPosition = n;
                notifyDataSetChanged();
            }
        });
        holder.zuohuaTv.setSelected(position == selectPosition);
        holder.zuohuaEidt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
/*                if(menuIsOpen()){
                    closeMenu();
                }else{*/
                closeMenu();
                int n = holder.getLayoutPosition();
                mZuohuaSlidingViewClickListener.onEditBtnClick(view, n);
                // }
            }
        });
        holder.zuohuaDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
/*                if(menuIsOpen()){
                    closeMenu();
                }else{*/
                closeMenu();
                int n = holder.getLayoutPosition();
                mZuohuaSlidingViewClickListener.onDelBtnCilck(view, n, bfList);

                // }
            }
        });
    }


    @Override
    public int getItemCount() {
        return bfList.size();
    }

    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlideBtnItemView) view;
    }

    @Override
    public void onDownOrMove(SlideBtnItemView mSlideBtnItemView) {
        if (menuIsOpen()) {
            if (mMenu != mSlideBtnItemView) {
                closeMenu();
            }
        }
    }

    /**
     * 关闭菜单*/
    public void closeMenu() {
        if (mMenu != null){
            mMenu.closeMenu();
            mMenu = null;
        }
    }

    /**
     * 清除选中状态*/
    public void closeSelect(){
        selectPosition = -1;
        notifyDataSetChanged();
    }

    /**
     * 判断是否有菜单打开*/
    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }


    class zuohuaViewHolder extends RecyclerView.ViewHolder {
        private TextView zuohuaTv;
        private ImageView zuohuaEidt;
        private ImageView zuohuaDel;
        private RelativeLayout zuohuaRel;
        private SlideBtnItemView zuohuaS;

        public zuohuaViewHolder(View itemView) {
            super(itemView);
            zuohuaTv = itemView.findViewById(R.id.zuohua_item_tv);
            zuohuaEidt = itemView.findViewById(R.id.zuohua_item_edit);
            zuohuaDel = itemView.findViewById(R.id.zuohua_item_del);
            zuohuaRel = itemView.findViewById(R.id.zuohua_item_rel);
            zuohuaS = itemView.findViewById(R.id.zuohua_slidebtnItemView);
            zuohuaS.setSlidingButtonListener(HycZhAdapter.this);

        }
    }

    public interface ZuohuaSlidingViewClickListener {
        void onItemClick(View view, int position);

        void onDelBtnCilck(View view, int position, List<Hyc> bfList);

        void onEditBtnClick(View view, int position);
    }
}
